package com.snail.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.snail.model.vo.user.ThirdPartyUserInfoVO;
import com.snail.service.ThirdPartyProviderService;
import org.springframework.stereotype.Service;

/**
 * @Author 蔡徐坤
 * @Date 2025/8/15 15:55
 * @Description: TODO
 */
@Service("ThirdPartyProviderService")
public class ThirdPartyProviderServiceImpl implements ThirdPartyProviderService {
    @Override
    public ThirdPartyUserInfoVO getUserInfoByCode(String provider, String code) {
            // 模拟第三方的“code”校验：过短或为空视为失败
            if (code == null || code.isBlank() || code.length() < 4) {
                return null;
            }

            ThirdPartyUserInfoVO info = new ThirdPartyUserInfoVO();

            // 生成“短 openId”（可复现，便于测试；真实场景请用第三方返回的 openId）
            String shortOpenId = DigestUtil.md5Hex(provider + ":" + code).substring(0, 16);
            info.setOpenId(shortOpenId);

            // 第三方昵称（此处模拟：若第三方未返回，则给短昵称）
            info.setNickname("用户" + RandomUtil.randomNumbers(4));

            // 第三方头像（此处模拟默认）
            info.setAvatar("http://172.172.2.99:9000/pic/2025/08/19/ab115febdd2f06da6308e8bddfb01edf.jpg");

            info.setProvider(provider);
            return info;
        }
}
